/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import Entity.ModelCar;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrKoi
 */
public class ModelCarBean {
    Collection listMC,listEdit;
    Connect c=new Connect();
    CallableStatement cl=null;
    Connection con;
    public ModelCarBean() {
    }
     private void closeCnn(){
        try {
            cl.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public boolean CreateModelCar(String modelname)
    {
        con=c.getConnection();
       try
       {
           cl=con.prepareCall("{call CreateModelCar_sp(?)}");
           cl.setString(1,modelname);
           int rs=cl.executeUpdate();
           if(rs >0)
           {
               return true;
           }
          closeCnn();
       }catch (Exception ex){closeCnn();return false;}
        return false;
    }
    //select list modelcar
    public Collection ListModelCar()
    {
        con=c.getConnection();
        listMC=new ArrayList();
        try
        {
            cl=con.prepareCall("{call ListModelCar_sp}");
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                ModelCar mc=new ModelCar();
                mc.setModelID(rs.getInt("ModelID"));
                mc.setModelName(rs.getString("ModelName"));   
                listMC.add((Object)mc);
            }
            closeCnn();
        }catch (Exception ex){closeCnn();}
        return listMC;
    }
    //select model car edit
     public Collection ModelCarEditDrop(String modelname)
    {
        con=c.getConnection();
        listEdit=new ArrayList();
        try
        {
            cl=con.prepareCall("{call ModelCarEditDrop_sp(?)}");
            cl.setString("modelname", modelname);
            ResultSet rs=cl.executeQuery();
            while(rs.next())
            {
                ModelCar mc=new ModelCar();
                if(rs.getString("ModelName").equals(modelname))
                {
                    mc.setEditDrop("selected");
                }
                else
                {
                    mc.setEditDrop("");
                } 
                mc.setModelID(rs.getInt("ModelID"));
                mc.setModelName(rs.getString("ModelName"));   
                listEdit.add((Object)mc);
            }
            closeCnn();
        }catch (Exception ex){closeCnn();}
        return listEdit;
    }
    public boolean DeleteModelCar(int id)
    {
        con=c.getConnection();
        try
        {
            cl=con.prepareCall("{call DeleteModelCar_sp(?)}");
            cl.setInt(1,id);
            int rs=cl.executeUpdate();
            if(rs >0)
            {
                return true;
            }
            closeCnn();
        }catch (Exception ex){closeCnn();return false;}
        return false;
    }
}
